/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : WordUtil.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 11, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lwf.number.util.RandomNumberGenerator;

import lwf.factory.PropertyReader;
import lwf.factory.WordImageFactory;

/**
 * Type the class description here
 *  
 */
public class WordUtil {
	//-----------------------------------------------------------------------------------
	//Word Characteristics
	//Very Simple
	private static final int VERY_SIMPLE_WORD_CHARS_RANGE_START = 1;
	private static final int VERY_SIMPLE_WORD_CHARS_RANGE_END   = 2;
	//Simple
	private static final int SIMPLE_WORD_CHARS_RANGE_START = 3;
	private static final int SIMPLE_WORD_CHARS_RANGE_END   = 3;
	//
	private static final int MEDIUM_WORD_CHARS_RANGE_START = 4;
	private static final int MEDIUM_WORD_CHARS_RANGE_END   = 5;
	//
	private static final int OVER_MEDIUM_WORD_CHARS_RANGE_START = 6;
	private static final int OVER_MEDIUM_WORD_CHARS_RANGE_END   = 7;
	//------------------------------------------------------------------------------------
	
	public static String getBeginningLetter(String word) {
		if(word != null && word.trim().length() != 0) {
			return word.substring(0,1);
		}
		return null;
	}
	
	public static String getEndingLetter(String word) {
		if(word != null) {
			int len = word.length();
			if(len != 0) {
				return word.substring(len-1,len);	
			}
			
		}
		return null;
	}
	
	public static String getWordSentence(String word) {
		String sentence = "";
		Map mapsentences = PropertyReader.getInstance().getWordSentences();
		if (mapsentences.containsKey(word)) {
			List lstSentences = (List) mapsentences.get(word);
			int pickSentenceIndex = 0;
			if (lstSentences.size() > 1) {
				pickSentenceIndex = RandomNumberGenerator.generateInt(1,	lstSentences.size()) - 1;
			}
			sentence = (String) lstSentences.get(pickSentenceIndex);
		}
		return sentence;
	}
	
	//-----------------------------------------------------------------------------------
	// Identify word type
	//-----------------------------------------------------------------------------------
	
	public static boolean isVerySimple(String word) {
		if(word != null) {
			int len = word.trim().length();
			if(len == VERY_SIMPLE_WORD_CHARS_RANGE_START ||
			   len == VERY_SIMPLE_WORD_CHARS_RANGE_END) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isSimple(String word) {
		if(word != null) {
			int len = word.trim().length();
			if(len == SIMPLE_WORD_CHARS_RANGE_START ||
			   len == SIMPLE_WORD_CHARS_RANGE_END) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isMediumWord(String word) {
		if(word != null) {
			int len = word.trim().length();
			if(len == MEDIUM_WORD_CHARS_RANGE_START ||
			   len == MEDIUM_WORD_CHARS_RANGE_END) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isAboveMediumWord(String word) {
		if(word != null) {
			int len = word.trim().length();
			if(len == OVER_MEDIUM_WORD_CHARS_RANGE_START ||
			   len == OVER_MEDIUM_WORD_CHARS_RANGE_END) {
				return true;
			}
		}
		return false;
	}	
	
	public static boolean isHardWord(String word) {
		if(word != null) {
			int len = word.trim().length();
			if(len > OVER_MEDIUM_WORD_CHARS_RANGE_END) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Gets a random image word from the filtered image word information.
	 * 
	 * @return
	 */
	public static String getAImageWord() {
		List availablefilterdwords = WordImageFactory.getInstance().getImageWords();
		int randword = RandomNumberGenerator.generate(1, availablefilterdwords.size());
		return (String) availablefilterdwords.get(randword-1);
	}
	
	public static List removeDuplicates(List data) {
		List newdata = null;
		if(data != null && data.size() != 0) {
			newdata = new ArrayList();
			Map mapdata = new HashMap();
			for(int i=0; i<data.size(); i++) {
				String value = (String) data.get(i);
				if(!mapdata.containsKey(value)) {
					newdata.add(value);
					mapdata.put(value, value);
				}
			}
		}
		return newdata;
	}
	
    public static String getWordData(String word, boolean isImageFirst) {
    	String image = ImageHtml.getWordImage(word);
		StringBuffer sbWData = new StringBuffer();
		sbWData.append("<table   width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
			sbWData.append("<tr>");
			    if(image.length() != 0) {
			    	if(!isImageFirst) {
						sbWData.append("<td   width=\"40%\" class=\"dataleft\">");
							sbWData.append(word);
					    sbWData.append("</td>");
					    sbWData.append("<td  width=\"10%\"> &nbsp;</td>");
					    sbWData.append("<td  width=\"50%\">");
							sbWData.append(image);
					    sbWData.append("</td>");			    		
			    	}
			    	else {
			    		sbWData.append("<td  width=\"50%\">");
							sbWData.append(image);
					    sbWData.append("</td>");
					    sbWData.append("<td  width=\"10%\"> &nbsp;</td>");
					    sbWData.append("<td   width=\"40%\" class=\"dataleft\">");
					    	sbWData.append(word);
				        sbWData.append("</td>");
			    	}

			    }
			    else {
					sbWData.append("<td class=\"dataleft\" colspan=\"3\" >");
						sbWData.append(word);
					sbWData.append("</td>");
			    }
			sbWData.append("</tr>");
		sbWData.append("</table>");

		return sbWData.toString();
    }

}

